export const getTime = () => {
    let message = ''
    const hours = new Date().getHours()
    switch (true) {
        case hours < 8 && hours > 0:
            message = '早上'
            break
        case hours < 11 && hours >= 8:
            message = '上午'
            break
        case hours < 14 && hours >= 11:
            message = '中午'
            break
        case hours < 18 && hours >= 14:
            message = '下午'
            break
        case hours < 24 && hours >= 18:
            message = '晚上'
            break
        default:
            break
    }
    return message
}

/**
 * 将时间戳转化成年月日时分秒
 * @param date 当前时间戳
 * @param format  返回的时间格式，默认格式： 'YYYY年MM月DD日 hh:mm:ss'
 * @returns 返回指定格式的当前时间，示例：'YYYY年MM月DD日 hh:mm:ss' 或者 'YYYY-MM-DD hh:mm:ss'
 */
export const format = (date: Date, format?: string): string => {
    format = format ?? 'YYYY年MM月DD日 hh:mm:ss'
    let time = ''
    let yearNext = ''
    let monthNext = ''
    let dayNext = ''
    let hourNext = ''
    let minuteNext = ''

    if (format[4] === '-') {
        yearNext = format[4]
        monthNext = format[7]
        dayNext = format[10]
        hourNext = format[13]
        minuteNext = format[16]
    } else {
        yearNext = format[4]
        monthNext = format[7]
        dayNext = format[10] + format[11]
        hourNext = format[14]
        minuteNext = format[17]
    }

    const year = date.getFullYear()
    const month = ('0' + (date.getMonth() + 1)).slice(-2) // 月份从0开始，所以需要+1，并补零
    const day = ('0' + date.getDate()).slice(-2)
    const hour = ('0' + date.getHours()).slice(-2)
    const minute = ('0' + date.getMinutes()).slice(-2)
    const second = ('0' + date.getSeconds()).slice(-2)

    switch (format) {
        case `YYYY${yearNext}MM${monthNext}DD${dayNext}hh${hourNext}mm${minuteNext}ss`:
            time = year + yearNext + month + monthNext + day + dayNext + hour + hourNext + minute + minuteNext + second
            break
        case `YYYY${yearNext}MM${monthNext}DD${dayNext}hh${hourNext}mm`:
            time = year + yearNext + month + monthNext + day + dayNext + hour + hourNext + minute
            break
        case `YYYY${yearNext}MM${monthNext}DD${dayNext}hh`:
            time = year + yearNext + month + monthNext + day + dayNext + hour
            break
        case `YYYY${yearNext}MM${monthNext}DD${dayNext}`:
            time = year + yearNext + month + monthNext + day + dayNext
            break
        case `YYYY${yearNext}MM${monthNext}`:
            time = year + yearNext + month + monthNext
            break
        case `YYYY${yearNext}`:
            time = year + yearNext
            break
        default:
            break
    }
    return time
}
